import {login,register} from '@/http/requestFunction'

export default {
    namespaced:true,
    actions:{
        // 登陆
        async login(content,user){
            try{
                if(user.username === '' || user.password === ''){
                    content.commit('TipsMsg','输入不可为空')
                }else{
                    const res = await login(user.username,user.password)
                    content.commit('TipsMsg',res.msg)
                    if(res.data){
                        sessionStorage.setItem('user',JSON.stringify(res.data))
                    }
                }
                
            }catch(e){
                console.log(e)
            }
        },

        // 注册
        async register(content,user){
            try{
                if(user.username === '' || user.password === ''){
                    content.commit('TipsMsg','输入不可为空')
                }else{
                    const res = await register(user.username,user.password)
                    content.commit('TipsMsg',res.msg)
                }
                
            }catch(e){
                console.log(e)
            }
        }


    },
    mutations:{
        TipsMsg(state,msg){
            state.tipsMsg = msg
            state.tipShow = true
            setTimeout(()=>{
                state.tipShow = false
                state.tipsMsg = ''
            },1500)
        }
    },
    state:{
        tipsMsg:"",
        tipShow:false
        
    }

}